export default {
  install(Vue) {
    const contexts = require.context('../constants', false, /\.js$/)
    const contexts2 = require.context('./modules', false, /\.js$/)
    function registerFiltersFromContext(contexts) {
      contexts.keys().forEach((filter) => {
        let filterEntity = contexts(filter).default || contexts(filter)
        if (typeof filterEntity == 'function') {
          const perpoty = filter.replace(/^\.\//, '').replace(/\.\w+$/, '')
          filterEntity = { [perpoty]: filterEntity }
        }
        Object.keys(filterEntity).forEach((name) => {
          if (Array.isArray(filterEntity[name])) {
            const arrFilter = filterEntity[name]
            const filterFun = (val) => {
              return (
                arrFilter.find((item) => {
                  return item.value === val
                })?.label || ''
              )
            }
            Vue.filter(name + 'Filter', filterFun)
          } else if (typeof filterEntity[name] === 'function') {
            Vue.filter(name, filterEntity[name])
          }
        })
      })
    }
    registerFiltersFromContext(contexts)
    registerFiltersFromContext(contexts2)
  }
}
